package com.hantang.web.base;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import org.apache.log4j.Logger;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
import org.glassfish.grizzly.threadpool.ThreadPoolConfig;

import com.hantang.web.config.JsonConfig;

public class MainHttpsService {

	private static Logger logger = Logger.getLogger(MainService.class);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		  HttpServer server = HttpServer.createSimpleServer();
	       server.getServerConfiguration().addHttpHandler(new HttpHandler() {

	           @Override
	           public void service(Request request, Response resp) throws Exception {
	               System.out.println("Got request");
	               resp.getWriter().write("Test");
	           }
	       }, "/test");
	       
	       NetworkListener sslListener = makeNetworkListener();
	       ThreadPoolConfig threadPoolConfig = ThreadPoolConfig
			        .defaultConfig()
			        .setCorePoolSize(JsonConfig.getInstance().getCorePoolSize())
			        .setMaxPoolSize(JsonConfig.getInstance().getMaxiumPoolSize());
	       sslListener.getTransport().setWorkerThreadPoolConfig(threadPoolConfig);
	       server.addListener(sslListener);
	       
	       try {
	           server.start();
	           System.in.read();
	       } catch (IOException ex) {
	    	   logger.error(ex);
	       }
	   }
	
	
	private static NetworkListener makeNetworkListener() {
		NetworkListener sslListener = new NetworkListener("SSL", "0.0.0.0", 8043);
	       sslListener.setSecure(true);

	       Provider[] providers = Security.getProviders();
	       for(Provider p:providers){
	           System.out.println(p+","+p.getName()+","+p.getInfo());
	       }
	       try {
	           SSLContext context = SSLContext.getInstance("TLS");
	           KeyStore ks = KeyStore.getInstance("JKS");
	           KeyStore ts = KeyStore.getInstance("JKS");
	           char[] pass = "i*23_0".toCharArray();
	           char[] keypass = "s7e*3d".toCharArray();
	           ks.load(new FileInputStream("/Users/yueyulin/tmp/keystore"), pass);
	           ts.load(new FileInputStream("/Users/yueyulin/tmp/teastore"), pass);
	           KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
	           kmf.init(ks, keypass);
	           TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
	           tmf.init(ts);
	           context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

	           System.out.println("default is "+context.getProtocol()+","+context.getProvider());
	           SSLEngineConfigurator configurator = new SSLEngineConfigurator(context);
	           configurator.setNeedClientAuth(false);
	           configurator.setClientMode(false);
	           sslListener.setSSLEngineConfig(configurator);
	       } catch (Exception ex) {
	    	   logger.error(ex);
	       }
		return sslListener;
	}

}
